/**
 * Created by zlj on 2016/11/6
 */
require('libs/validator');
var $ = require('jquery');
//opt 对象 ，rule 规则 ,msg对应显示信息
 module.exports = function(opt,rule,msg,asyfun,callback) {
    var theform = $(opt.selector);
    var btn = theform.find('#submit_form');
    var action = theform.attr('action');
    theform.validate({
        rules:rule,
        messages:msg,
        submitHandler: function() {
            // 已经通过了验证
            btn.button('loading');
            var data = asyfun();
            if(data==false){
                return false;
            }
            $
                .ajax(action, {
                    method: 'POST',
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(data)
                })
                .then(function(response) {
                    callback(response);
                    btn.button('reset');
                });
        }
    });
};